/**
 * 版权所有：厦门市巨龙信息科技有限公司
 * Copyright 2023 Xiamen Dragoninfo Eng. Co. Ltd.
 * All right reserved.
 */
package com.github.alfred.handler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 类说明
 *
 * @author chengh
 * @date 2023/12/26 8:41
 */
public class BeanHandler {

    private BeanHandler() {

    }

    /**
     *  map 转对象
     * @param map map
     * @param clazz clazz
     * @param <T> T
     * @return T
     */
    public static <T> T mapToBean(Map<String, Object> map, Class<T> clazz) {
        Map<String, Object> resultMap = LowKeyHandler.toLowerCamelCase(map);
        return BeanUtil.mapToBean(resultMap, clazz, buildCopyOptions());
    }

    /**
     * List<Map<String,Object>> 转对List<T>
     * @param mapList mapList
     * @param clazz clazz
     * @param <T> T
     * @return List
     */
    public static <T> List<T> listMapToBeanList(List<Map<String, Object>> mapList, Class<T> clazz) {
        List<T> tList = new ArrayList<>();
        for (Map<String, Object> map : mapList) {
            tList.add(mapToBean(map, clazz));
        }
        return tList;
    }

    private static CopyOptions buildCopyOptions() {
        CopyOptions copyOptions = new CopyOptions();
        copyOptions.setIgnoreCase(true);
        copyOptions.setIgnoreError(true);
        return copyOptions;
    }
}
